package cn.xx.product.service.impl;

import cn.xx.basic.service.impl.BaseServiceImpl;
import cn.xx.product.domain.Product;
import cn.xx.product.domain.ProductDetail;
import cn.xx.product.mapper.ProductDetailMapper;
import cn.xx.product.mapper.ProductMapper;
import cn.xx.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private ProductMapper productMapper;

    // 重写保存
    @Override
    public void save(Product product) {
        // 默认设置状态为下架
        product.setState(0);
        // 默认设置库存0
        product.setSalecount(0L);
        // 调用父类方法保存product
        super.save(product);
        // 取出productDetail
        ProductDetail productDetail = product.getProductDetail();
        // 给外键赋值
        productDetail.setProduct(product);
        // 保存productDetail
        productDetailMapper.save(productDetail);
    }

    // 重写修改
    @Override
    public void update(Product product) {
        // 调用父类方法修改product
        super.update(product);
        // 取出productDetail
        ProductDetail productDetail = product.getProductDetail();
        // 设置外键id
        productDetail.setProduct(product);
        // 修改productDetail
        productDetailMapper.update(productDetail);
    }

    @Override
    public void onSale(Long[] ids) {
        productMapper.onSale(ids);
    }

    @Override
    public void offSale(Long[] ids) {
        productMapper.offSale(ids);
    }


    @Override
    public void delete(Long id) {
        productDetailMapper.delete(id);
        super.delete(id);
    }

    @Override
    public void patchDel(Long[] ids) {
        // 两张表都要删除
        productDetailMapper.patchDel(ids);
        productMapper.patchDel(ids);
    }
}
